package com.onepice.mlp.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.onepice.mlp.core.service.TagdataService;
import com.onepice.mlp.data.entity.Tagdata;
import com.onepice.mlp.utils.commom.ResponseResult;
import com.onepice.mlp.utils.enums.CodeType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author FireWang
 * @date 2024/11/11 22:50:13
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/tagdata")
public class TagdataController {
    private TagdataService tagdataService;
    private static JSONObject result;

    /**
     * 获取全部数据列表
     *
     * @return
     */
    @PostMapping("/list")
    public JSONObject getAll(@RequestParam String envType) {
        try {
            String[] envTypes = envType.split(",");
            List<Tagdata> tagdataList = tagdataService.getAll();
            List<Tagdata> removeList = new ArrayList<>();
            for (Tagdata tagdata : tagdataList) {
                String dataEnvType = tagdata.getEnvType();
                boolean has = false;
                for (String type : envTypes) {
                    if (dataEnvType.contains(type)) {
                        has = true;
                    }
                }
                if (!has) {
                    removeList.add(tagdata);
                }
            }
            tagdataList.removeAll(removeList);

            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("list", tagdataList);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @GetMapping
    public JSONObject getById(@RequestParam Integer id) {
        try {
            Tagdata tagdata = tagdataService.getById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("data", tagdata);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 新建
     *
     * @param tagdata
     * @return
     */
    @PostMapping
    public JSONObject insert(@RequestBody Tagdata tagdata) {
        try {
            log.info("[insert]tagdata: " + tagdata);
            Tagdata paraData = tagdataService.getByLngLat(tagdata.getTestPointLng(), tagdata.getTestPointLat());
            if (paraData != null) {
                log.info("[insert]已存在，更新paraData: " + paraData);
                tagdata.setId(paraData.getId());
                tagdataService.update(tagdata);
            } else {
                tagdataService.insert(tagdata);
            }
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");

            // 返回最新数据
            tagdata = tagdataService.getByLngLat(tagdata.getTestPointLng(), tagdata.getTestPointLat());
            result.put("tagdata", tagdata);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新
     *
     * @param tagdata
     * @return
     */
    @PutMapping
    public JSONObject update(@RequestBody Tagdata tagdata) {
        try {
            tagdataService.update(tagdata);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public JSONObject deleteById(@RequestParam Integer id) {
        try {
            tagdataService.deleteById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据坐标获取
     *
     * @param lng
     * @param lat
     * @return
     */
    @PostMapping(value = "/getByLngLat")
    public JSONObject getByLngLat(@RequestParam String lng, @RequestParam String lat) {
        try {
            log.info("lng: " + lng + ", lat: " + lat);
            Tagdata tagdata = tagdataService.getByLngLat(lng, lat);
            if (tagdata != null) {
                result = ResponseResult.build(CodeType.SUCCESS, "成功！");
                result.put("tagdata", tagdata);
                log.info("tagdata: " + tagdata);
            } else {
                result = ResponseResult.build(CodeType.FAIL, "失败！");
            }
            log.info("result: " + result);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据坐标删除
     *
     * @param lng
     * @param lat
     * @return
     */
    @PostMapping(value = "/deleteByLngLat")
    public JSONObject deleteByLngLat(@RequestParam String lng, @RequestParam String lat) {
        try {
            Tagdata tagdata = tagdataService.getByLngLat(lng, lat);
            if (tagdata != null) {
                tagdataService.deleteById(tagdata.getId());
            }
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("data", tagdata);
            log.info("result: " + result);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }
}
